package com.jin.lazytool.handler;

import java.time.LocalDateTime;

public class TimerLoopHandler extends ILoopHandler{
    private long timer;
    private LocalDateTime timerToDateTime;
    private Integer jumpCount;
    public TimerLoopHandler(){}
    private TimerLoopHandler(long timer,Integer jumpCount){
        this.timer = timer;
        this.jumpCount = jumpCount;
    }

    @Override
    public Integer execute(Integer index) throws InterruptedException{
        if(timerToDateTime == null){
            timerToDateTime = LocalDateTime.now().plusSeconds(timer);
        }
        if(this.timerToDateTime.compareTo(LocalDateTime.now()) < 0){
            Thread.sleep(time);
            return index;
        }
        return index + jumpCount;
    }

    @Override
    public ILoopHandler newHandler(Integer count, long timer, LocalDateTime dateTime, String js,Integer jumpCount) {
        return new TimerLoopHandler(timer,jumpCount);
    }
}
